package com.yeehot.iot_manager.adapter;

import android.content.Context;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.yeehot.iot_manager.DetailActivity;
import com.yeehot.iot_manager.MainActivity;
import com.yeehot.iot_manager.R;
import com.yeehot.iot_manager.bean.Device;
import com.yeehot.iot_manager.ui.login.LoginActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class RefreshAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    Context        mContext;
    LayoutInflater mInflater;
    List<Device>   mDatas=new ArrayList<>();


    public RefreshAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = mInflater.inflate(R.layout.item_device_info, parent, false);

        return new ItemViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if(holder instanceof ItemViewHolder){

            ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
            Device device = mDatas.get(position);
            itemViewHolder.deviceName.setText(device.getDeviceName());
            itemViewHolder.detail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(mContext, DetailActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putString("deviceName",device.getDeviceName());
                    intent.putExtras(bundle);
                    mContext.startActivity(intent);


                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @Override
    public int getItemViewType(int position) {

        return super.getItemViewType(position);
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.deviceName)
        TextView deviceName;

        @BindView(R.id.detail)
        Button detail;
        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            initListener(itemView);
        }

        private void initListener(View itemView) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(mContext, "poistion "+ getAdapterPosition(), Toast.LENGTH_SHORT).show();
                }
            });
        }
    }


    public void AddHItem(List<Device> items){
        System.out.println("c实时"+items.size());
        mDatas.clear();
        mDatas.addAll(items);
        notifyDataSetChanged();
    }
    public void CleanData(){

        mDatas.clear();
        notifyDataSetChanged();
    }

    public void AddHeaderItem(List<Device> items){
        mDatas.addAll(0,items);
        notifyDataSetChanged();
    }

    public void AddFooterItem(List<Device> items){
        mDatas.addAll(items);
        notifyDataSetChanged();
    }
}